<?php
    use yii\helpers\Html;
    use yii\helpers\Url;
    use yii\widgets\ActiveForm;
?>
<style>
    .table .permission_th{width:120px;}
    .table .td2{width:120px;}
    .table .permission_th label{cursor: pointer}
    .table .permission_td label{margin-right:30px;font-weight: normal;cursor: pointer}
    .table .permission_td label input{vertical-align:middle;margin-top:0px;}
    .table .permission_td label span{vertical-align:middle;}
</style>

<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox ">
                <div class="ibox-title">
                    <h5><?=Html::a('<i class="fa fa-angle-left"></i> 返回','javascript:history.go(-1);',['class'=>'btn btn-xs btn-white'])?> 分配权限</h5>
                </div>
                <div class="ibox-content">
                    <?php if($list){ ?>
                    <table class="table table-hover table-bordered">
                        <?php foreach($list as $key=>$item){ ?>
                            <?php if(isset($item['child'])) foreach($item['child'] as $k=>$val){?>
                                <tr class="tr<?=($key+1)?>">
                                    <?php if($k==0){ ?>
                                    <th class="permission_th" rowspan="<?=count($item['child'])?>">
                                        <label><input type="checkbox" name="id[]" value="<?=$item['menu_code']?>" <?=isset($item['active']) && $item['active']==1?'checked':''?> > <span><?=$item['name']?></span></label>
                                    </th>
                                    <?php }?>
                                    <td class="permission_td td2">
                                        <label><input type="checkbox" name="id[]" value="<?=$val['menu_code']?>" <?=isset($val['active']) && $val['active']==1?'checked':''?> > <span><?=$val['name']?></span></label>
                                    </td>
                                    <td class="permission_td">
                                        <?php if(isset($val['child'])) foreach($val['child'] as $v){ ?>
                                            <label><input type="checkbox" name="id[]" value="<?=$v['menu_code']?>" <?=isset($v['active']) && $v['active']==1?'checked':''?> > <span><?=$v['name']?></span></label>
                                        <?php }?>
                                    </td>
                                </tr>
                            <?php }else{?>
                                <tr class="tr<?=($key+1)?>">
                                    <th class="permission_th">
                                        <label><input type="checkbox" name="id[]" value="<?=$item['menu_code']?>" <?=isset($item['active']) && $item['active']==1?'checked':''?> > <span><?=$item['name']?></span></label>
                                    </th>
                                    <td class="permission_td td2">
                                    </td>
                                    <td class="permission_td">
                                    </td>
                                </tr>
                        <?php }}?>
                    </table>
                    <div class="form-group" style="padding: 5px 0 30px 0;">
                        <div class="col-sm-4 col-sm-offset-4">
                            <input type="button" class="btn btn-primary col-md-offset-1 pole_permission_save" value="保存"/>
                            <a href="javascript:history.go(-1);" type="button" class="btn btn-white col-md-offset-1" >返回</a>
                        </div>
                    </div>
                    <?php }?>
                </div>
            </div>
        </div>
    </div>
    <script>
        $(function(){
            //父级菜单默认选中
            $('.permission_td input[type="checkbox"]').change(function(){
                if($(this).prop("checked")){
                    $(this).parents('tr').find('.permission_th input[type="checkbox"]').prop('checked', $(this).prop('checked'));
                    $(this).parents('tr').find('.td2 input[type="checkbox"]').prop('checked', $(this).prop('checked'));
                }
            });
            // //全选,反选
            // $('.permission_th input[type="checkbox"]').change(function(){
            //     $(this).parents('tr').find('.permission_td input[type="checkbox"]').prop('checked',$(this).prop('checked'));
            // });
            // $('.td2 input[type="checkbox"]').change(function(){
            //     $(this).parents('tr').find('.permission_td input[type="checkbox"]').prop('checked',$(this).prop('checked'));
            // });
            //保存
            $('.pole_permission_save').click(function(){
                var _data = 'role_id=<?=$id?>&';
                    _data += $('table input[name="id[]"]:checked').serialize();
                $.post("role-permission-edit-ajax-do", _data,
                    function(data){
                        if(data.code == 20000){
                            swal("保存成功！", data.message, "success");
                            setTimeout(function(){window.location.href="/admin-role-permission/role-list"},1500);
                        }else{
                            swal("保存失败！", data.message, "warning");
                        }
                }, "json");
            });
        });
    </script>